package com.huirui.server.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huirui.server.context.UserContext;
import com.huirui.server.mapper.BrowseHistoryMapper;
import com.huirui.server.pojo.entity.BrowseHistory;
import com.huirui.server.pojo.vo.BrowseHistoryAnotherVo;
import com.huirui.server.pojo.vo.BrowseHistoryVo;
import com.huirui.server.service.BrowseHistoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author yujun
 * @description 针对表【t_browse_history(用户浏览记录表)】的数据库操作Service实现
 * @createDate 2025-09-18 16:24:15
 */
@Service
@Slf4j
public class BrowseHistoryServiceImpl extends ServiceImpl<BrowseHistoryMapper, BrowseHistory>
        implements BrowseHistoryService {

    @Override
    public List<BrowseHistoryVo> listUserBrowseHistory() {
        Long userId = UserContext.getId();
        LinkedHashMap<LocalDate, List<BrowseHistoryAnotherVo>> collect = baseMapper
                .queryUserBrowseHistory(userId)
                .stream()
                .collect(Collectors.groupingBy(BrowseHistoryAnotherVo::getBrowseDate, LinkedHashMap::new, Collectors.toList()));
        List<BrowseHistoryVo> res = new ArrayList<>();
        collect.forEach((k, v) -> {
            BrowseHistoryVo historyVo = new BrowseHistoryVo();
            historyVo.setDate(k);
            historyVo.setList(v);
            res.add(historyVo);
        });
        return res;
    }
}




